package com.nhjf.dao.teachers;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.nhjf.dao.BaseDao;
import com.nhjf.model.teachers.RecentlyStudied;
import com.nhjf.pojo.qo.teachers.RecentlyStudiedQo;

@Repository
public class RecentlyStudiedDao extends BaseDao<RecentlyStudied, RecentlyStudiedQo>{

	@Override
	protected Criteria buildCriteria(Criteria criteria, RecentlyStudiedQo qo) {
		// TODO Auto-generated method stub
		if(qo!=null) {
			criteria.setFetchMode("courses", qo.isSeachCourses()?FetchMode.JOIN:FetchMode.SELECT);	
			criteria.setFetchMode("user", qo.isSeachUser()?FetchMode.JOIN:FetchMode.SELECT);	
			criteria.setFetchMode("teacher", qo.isSeachTeacher()?FetchMode.JOIN:FetchMode.SELECT);	
			if(qo.getCoursesId()!=null) {
				
				criteria.add(Restrictions.eq("courses.id",qo.getCoursesId()));
			}
			if(qo.getTeacherId()!=null) {
				criteria.add(Restrictions.eq("teacher.id",qo.getTeacherId()));
			}
			if(qo.getUserId()!=null) {
				
				criteria.add(Restrictions.eq("user.id",qo.getUserId()));
			}
			if(qo.getRecentLearnTime()!=null) {
				criteria.add(Restrictions.eq("recentLearnTime",qo.getRecentLearnTime()));
			}
			criteria.addOrder(Order.desc("recentLearnTime"));
		}
		return criteria;
	}

	@Override
	protected Class<RecentlyStudied> getEntityClass() {
		// TODO Auto-generated method stub
		return RecentlyStudied.class;
	}

}
